/*
 * Getrap
 * Copyright 2008(C) Grzegorz 'milyges' Glinski
 *
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
*/
#include <fMain.h>
#include <QTextCodec>
#include <QtDebug>
#include <fAbout.h>
#include <fAddFiles.h>
#include <fSettings.h>
#include <job.h>
#include <config.h>

FMain::FMain(QWidget * parent, Qt::WFlags f) : QMainWindow(parent, f)
{
 QAction * action;
 /* Inicjujemy interfejs */
 ui.setupUi(this);
 /* ZMieniamy rozmiary kolumn drzewka */
 ui.treeJobs->setColumnWidth(0, 200);
 ui.treeJobs->setColumnWidth(1, 80);
 ui.treeJobs->setColumnWidth(2, 70);
 ui.treeJobs->setColumnWidth(3, 60);
 ui.treeJobs->setColumnWidth(4, 70);
 ui.treeJobs->setColumnWidth(5, 70);
 ui.treeJobs->setColumnWidth(6, 300);
 /* Menu dla ikonki w tray'u */
 mTrayMenu = new QMenu(this);
 connect(mTrayMenu->addAction(tr("Dodaj...")), SIGNAL(triggered()), this, SLOT(addLinks()));
 //connect(mTrayMenu->addAction(tr("Ustawienia")), SIGNAL(triggered()), this, SLOT(settings()));
 mTrayMenu->addSeparator();
 connect(mTrayMenu->addAction(tr("Pokaż/Ukryj okno")), SIGNAL(triggered()), this, SLOT(trayActivated()));
 connect(mTrayMenu->addAction(tr("Wyjdź")), SIGNAL(triggered()), this, SLOT(close()));
 /* Ikonka w tray'u */
 mTrayIcon = new QSystemTrayIcon(QIcon(":/gfx/getrap.png"), this);
 mTrayIcon->setToolTip("Getrap");
 mTrayIcon->setContextMenu(mTrayMenu);
 connect(mTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));
 mTrayIcon->setVisible(true);
 /* Dodajemy przyciski toolbara'a */
 action = ui.toolBar->addAction(QIcon(":/gfx/add.png"), tr("Dodaj linki"));
 connect(action, SIGNAL(triggered()), this, SLOT(addLinks()));
 action = ui.toolBar->addAction(QIcon(":/gfx/remove.png"), tr("Usuń z listy"));
 connect(action, SIGNAL(triggered()), this, SLOT(removeItems()));
 action = ui.toolBar->addAction(QIcon(":/gfx/settings.png"), tr("Ustawienia"));
 connect(action, SIGNAL(triggered()), this, SLOT(settings()));
 /* Połącz pozostałe akcje */
 connect(ui.actAddFiles, SIGNAL(triggered()), this, SLOT(addLinks()));
 connect(ui.actRemove, SIGNAL(triggered()), this, SLOT(removeItems()));
 connect(ui.actAbout,  SIGNAL(triggered()), this, SLOT(about()));
 connect(ui.actSettings, SIGNAL(triggered()), this, SLOT(settings()));
 connect(ui.actStart, SIGNAL(triggered()), this, SLOT(startDownload()));
 connect(ui.actStop, SIGNAL(triggered()), this, SLOT(stopDownload()));
 connect(ui.actStopAll, SIGNAL(triggered()), this, SLOT(stopAll()));
 connect(ui.actStartAll, SIGNAL(triggered()), this, SLOT(startAll()));
}

FMain::~FMain()
{
 delete mTrayIcon;
 delete mTrayMenu;
}

void FMain::fatalError(QString msg)
{
 mTrayIcon->showMessage(tr("Błąd"), msg, QSystemTrayIcon::Critical);
 stopAll();
}

void FMain::downloadDone(QString fileName)
{
 mTrayIcon->showMessage(tr("Pobieranie pliku zakończone"), tr("Pomyślnie pobrano plik ") + fileName);
}

void FMain::addLinks(void)
{
 int i;
 QStringList links;
 Job * job;
 FAddFiles form(this);
 show();
 form.exec();
 if (form.result() == QDialog::Accepted)
 {
  links = form.getLinks();
  for(i=0;i<links.size();i++)
  {
   if (links[i].count(QRegExp("http\\://(.*\\.)?rapidshare.com/files/([0-9]*)/(.*)")) != 1)
      continue;
   job = new Job(ui.treeJobs, links[i]);
   connect(job, SIGNAL(fatalError(QString)), this, SLOT(fatalError(QString)));
   connect(job, SIGNAL(done(QString)), this, SLOT(downloadDone(QString)));
   if (Config::getValue("Autostart") == "1")
      job->start();
  }
 }
}

void FMain::removeItems(void)
{
 Job * job;
 int i;
 QList<QTreeWidgetItem *> selected = ui.treeJobs->selectedItems();
 for(i=0;i<selected.size();i++)
 {
  job = (Job *)selected[i];
  job->stop();
  delete job;
 }
}

void FMain::about(void)
{
 FAbout form(this);
 form.exec();
}

void FMain::settings(void)
{
 FSettings form(this);
 form.exec();
}

void FMain::startDownload(void)
{
 Job * job;
 int i;
 QList<QTreeWidgetItem *> selected = ui.treeJobs->selectedItems();
 for(i=0;i<selected.size();i++)
 {
  job = (Job *)selected[i];
  job->start();
 }
}

void FMain::stopDownload(void)
{
 Job * job;
 int i;
 QList<QTreeWidgetItem *> selected = ui.treeJobs->selectedItems();
 for(i=0;i<selected.size();i++)
 {
  job = (Job *)selected[i];
  job->stop();
 }
}

void FMain::startAll(void)
{
 Job * job;
 /* Pobieramy 1 element */
 job = (Job *)ui.treeJobs->topLevelItem(0);
 while(job)
 {
  job->start();
  job = (Job *)ui.treeJobs->itemBelow(job);
 }
}

void FMain::stopAll(void)
{
 Job * job;
 /* Pobieramy 1 element */
 job = (Job *)ui.treeJobs->topLevelItem(0);
 while(job)
 {
  job->stop();
  job = (Job *)ui.treeJobs->itemBelow(job);
 }
}

void FMain::trayActivated(QSystemTrayIcon::ActivationReason reason)
{
 if (reason == QSystemTrayIcon::DoubleClick)
    setVisible(!isVisible());
}
